package com.vil.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Village;
import com.vil.mapper.VillageMapper;
import com.vil.service.VillageService;

@Service
public class VillageServiceImpl implements VillageService {

    @Autowired
    private VillageMapper mapper;
    @Override
    public ServerResponse insertSelective(Village record) {
        record.setCreate_time(new Date());
        int i = mapper.insertSelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("新增成功");
        }
        return ServerResponse.buildErrorMsg("新增失败");
    }

    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        int i = mapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse updateByPrimaryKeySelective(Village record) {
        int i = mapper.updateByPrimaryKeySelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("修改成功");
        }
        return ServerResponse.buildErrorMsg("修改失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, Village village) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Village> list = mapper.getListByManyConditions(village);
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.buildSuccessData(pageInfo);
    }

//    @Override
//    public ServerResponse loginCheck(String name, String number) {
//        Village village = mapper.loginCheck(name, number);
//
//        System.out.println(village.toString());
//        if (village==null){
//            return ServerResponse.buildErrorMsg("该管理员不存在");
//        }
//        return ServerResponse.buildSuccessData(villgae);
//    }

    @Override
    public ServerResponse getListByManyCondition(Integer pageIndex, Integer pageSize, String name) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Village> list = mapper.getListByManyCondition(name);
        PageInfo villagePageInfo = new PageInfo(list);

        return ServerResponse.buildSuccessData(villagePageInfo);
    }

    @Override
    public ServerResponse getById(Long id) {
        Village village = mapper.selectByPrimaryKey(id);
        if (village==null){
            return ServerResponse.buildErrorMsg("不存在该管理员信息");
        }
        return ServerResponse.buildSuccessData(village);
    }

    @Override
    public ServerResponse deleteMany(Long[] ids) {
        int i = mapper.deleteMany(ids);
        if (i>0){
            return ServerResponse.buildSuccessMsg("批量删除成功");
        }
        return ServerResponse.buildErrorMsg("批量删除失败");
    }

    @Override
    public ServerResponse isExist(String name) {
        int exist = mapper.isExist(name);
        if (exist>0){
           return ServerResponse.buildErrorMsg("该登录名已存在，请重新输入");
        }
        return ServerResponse.buildSuccessMsg("该用户名可以使用");
    }

    @Override
    public ServerResponse getListVillage() {
        List<Village> list = mapper.getListVillage();
        return ServerResponse.buildSuccessData(list);
    }

    @Override
    public List<String> getListVillageName() {
        List<String> listVillageName = mapper.getListVillageName();
            return listVillageName;

    }

}
